﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLMB.Models;
using QLMB.ViewModels;
using System.Web.Script.Serialization;
using System.Web.UI.WebControls;

namespace QLMB.Controllers
{
    public class CustomerController : Controller
    {
        public IBookTicketRepository Repository { get; set; }

        public CustomerController()
            : this(new BookTicketRepository())
        {
        }

        public CustomerController(IBookTicketRepository repository)
        {
            Repository = repository;
        }

        public ActionResult ListCustomer(string sortOrder, string searchString)
        {
            if (String.IsNullOrEmpty(sortOrder))
                ViewBag.NameSortParm = "Name desc";
            else
                ViewBag.NameSortParm = "";
            var dshk = Repository.LayDSHKTT();
            if (!String.IsNullOrEmpty(searchString))
            {
                dshk = dshk.Where(s => s.HANHKHACH.CMND.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "Name desc":
                    dshk = dshk.OrderByDescending(s => s.HANHKHACH.Ten);
                    break;
                default:
                    dshk = dshk.OrderBy(s => s.HANHKHACH.Ten);
                    break;
            }
            return View(dshk.ToList());
        }


        public ActionResult Details(string mahk)
        {
            HANHKHACHTHANTHIET hk = Repository.LayHKTT(mahk);
            return View(hk);
        }

        [HttpGet]
        public ActionResult Edit(string mahk)
        {
            HANHKHACHTHANTHIET hk = Repository.LayHKTT(mahk);
            TempData["Mahk"] = hk.MaHK;
            return View(hk);
        }
        [HttpPost]
        public ActionResult Edit(HANHKHACHTHANTHIET hktt)
        {
            string mahk = TempData["Mahk"] as string;
            Repository.CapNhatHK(mahk, hktt);
            return RedirectToAction("ListCustomer","Customer");
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(HANHKHACHTHANTHIET hk)
        {
            Repository.TaoHK(hk);
            return RedirectToAction("ListCustomer", "Customer");
        }

        public ActionResult Delete(string mahk)
        {
            Repository.XoaHK(mahk);
            return RedirectToAction("ListCustomer", "Customer");
        }

        public ActionResult Log_Fight(string mahk)
        {
            IEnumerable<CT_VECHUYENBAY> ds_ctv = Repository.LayDSCTV(mahk);
            ViewBag.HKTT = Repository.LayHKTT(mahk);
            return View(ds_ctv.ToList());
        }
    }
}
